package com.zhiche.lisa.bms.service.psup.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.psup.PsupBaseFleetMapper;
import com.zhiche.lisa.bms.dao.model.psup.PsupBaseFleet;
import com.zhiche.lisa.bms.service.psup.PsupBaseFleetService;
import com.zhiche.lisa.core.supports.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 人送基础车队信息 服务实现类
 * </p>
 *
 * @author linbao
 * @since 2019-07-02
 */
@Service
@Slf4j
public class PsupBaseFleetServiceImpl extends ServiceImpl<PsupBaseFleetMapper, PsupBaseFleet> implements PsupBaseFleetService {

    /**
     * 分而查询
     *
     * @param page
     * @return
     */
    @Override
    public Page<PsupBaseFleet> queryBaseFleetPage(Page<PsupBaseFleet> page) {
        log.info("PsupBaseFleetServiceImpl.queryBaseFleetPage param: {}", page);
        EntityWrapper<PsupBaseFleet> ew = new EntityWrapper<>();
        ew.orderBy("id desc");
        Map<String, Object> condition = page.getCondition();
        if (Objects.nonNull(condition)) {
            Object fleetId = condition.get("fleetId");
            if (Objects.nonNull(fleetId) && StringUtils.isNotBlank(fleetId.toString())) {
                ew.eq("fleet_id", fleetId);
            }
            Object fleetName = condition.get("fleetName");
            if (Objects.nonNull(fleetName) && StringUtils.isNotBlank(fleetName.toString())) {
                ew.like("fleet_name", fleetName.toString());
            }
        }
        page.setRecords(baseMapper.selectPage(page, ew));
        return page;
    }

    /**
     * 新增
     *
     * @param psupBaseFleet
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void createBaseFleet(PsupBaseFleet psupBaseFleet) {
        log.info("PsupBaseFleetServiceImpl.createBaseFleet param: {}", psupBaseFleet);
        if (Objects.isNull(psupBaseFleet)) {
            throw new BaseException("参数为空");
        }
        if (Objects.isNull(psupBaseFleet.getFleetId()) || StringUtils.isBlank(psupBaseFleet.getFleetName())) {
            throw new BaseException("车队信息为空!");
        }
        boolean resultBoolean = this.verifyFleetIsExist(psupBaseFleet.getFleetId());
        if (resultBoolean) {
            throw new BaseException("当前车队信息已存在!");
        }
        this.insert(psupBaseFleet);
    }

    /**
     * 获取所有的维护的人送车队
     *
     * @return
     */
    @Override
    public List<PsupBaseFleet> getBaseFleet() {
        EntityWrapper<PsupBaseFleet> ew = new EntityWrapper<>();
        return this.selectList(ew);
    }

    /**
     * 判断车队id是否存在
     *
     * @param fleetId
     * @return 存在-true, 不存在=false
     */
    @Override
    public boolean verifyFleetIsExist(Long fleetId) {
        boolean resultBoolean = false;
        EntityWrapper<PsupBaseFleet> ew = new EntityWrapper<>();
        ew.eq("fleet_id", fleetId);
        int count = this.selectCount(ew);
        if (count > 0) {
            resultBoolean = true;
        }
        return resultBoolean;
    }
}
